<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>roller</title>
  <style>
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      margin: 0;
      font-family: Helvetica;
      font-size: 15px;
    }

    .roller-wrapper {
      width: 400px;
      height: 300px;
      position: relative;
      background-image: linear-gradient(to bottom, rgba(0, 0, 0, .3) 0%, rgba(0, 0, 0, 0) 25%, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, .3) 100%);
      box-shadow: inset 0px 0px 12px rgba(0, 0, 0, .2);
      border-radius: 12px;
    }

    .roller {
      list-style: none;
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
      overflow: auto;
      scroll-snap-type: y mandatory;

    }

    .roller li {
      width: 100%;
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      scroll-snap-align: start;
    }

    .roller-wrapper::after {
      content: '';
      position: absolute;
      top: 120px;
      width: 100%;
      height: 60px;
      background-image: linear-gradient(to bottom, rgba(0, 0, 0, .1) 0%, rgba(0, 0, 0, .2) 50%, rgba(0, 0, 0, .25) 50%, rgba(0, 0, 0, .1) 100%);
      border-top: 1px solid rgba(0, 0, 0, .12);
      border-bottom: 1px solid rgba(0, 0, 0, .12);
      pointer-events: none;
    }

    .roller li:last-child {
      margin-bottom: 120px;
    }
  </style>
</head>

<body>
  <div class="roller-wrapper">
    <ul class="roller">
      <li></li>
      <li></li>
      <li>iPhone 12 Pro</li>
      <li>iPhone 12</li>
      <li>iPhone 12 mini</li>
      <li>iPhone SE</li>
      <li>iPhone 11</li>
      <li>iPhone XR</li>
      <li>iPhone XS</li>
      <li>iPhone X</li>
    </ul>
  </div>

  <script>
    let rollerWrapper = document.querySelector('.roller-wrapper')
    let roller = document.querySelector('.roller')
    let itemList = document.querySelectorAll('.roller > li')
    console.log(itemList)
    const liHeight = itemList[0].offsetHeight
    roller.addEventListener('scroll', (e) => {
      // console.log(roller.scrollTop) 
      //60 120 180 为所获取到的高度，处以 li元素的高为当前元素
      const index = parseInt(roller.scrollTop / liHeight)
      console.log('this index is', index, 'this item text is ', itemList[index + 2].innerHTML)
    })
  </script>
</body>

</html>